package com.mtiiot.fstcpserver.mq.kafka.pool;

import org.apache.commons.pool2.BasePooledObjectFactory;
import org.apache.commons.pool2.PooledObject;
import org.apache.commons.pool2.impl.DefaultPooledObject;

/**
 * Created by Administrator on 2016/7/26.
 */
public class PooledKafkaProducerAppFactory extends BasePooledObjectFactory<KafkaProducerApp> {
    KafkaProducerAppFactory factory;

    public PooledKafkaProducerAppFactory(KafkaProducerAppFactory factory) {
        this.factory = factory;
    }

    public KafkaProducerApp create() throws Exception {
        KafkaProducerApp app = this.factory.newInstance();
        return app;
    }

    public PooledObject<KafkaProducerApp> wrap(KafkaProducerApp obj) {
        DefaultPooledObject pool = new DefaultPooledObject(obj);
        return pool;
    }

    public void destroyObject(PooledObject<KafkaProducerApp> p) throws Exception {
        ((KafkaProducerApp)p.getObject()).shutdown();
        super.destroyObject(p);
    }
}

